package cn.dankal.flyaudio.friend;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shehuan.niv.NiceImageView;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.encode.CodeCreator;

import cn.dankal.basiclib.DankalApplication;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.common.share.mob.ShareDialog2;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.Util;
import cn.dankal.basiclib.util.image.PicUtils;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.dialog.ShareDialog;
import cn.dankal.flyaudio.friend.utils.ShareUtils;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.GetGroupInfoCallback;
import cn.jpush.im.android.api.model.GroupInfo;

public class GroupQRCodeActivity extends BaseActivity {

    private ImageView qrCodeView;

    private NiceImageView avatarView;

    private TextView nameView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_qrcode;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("群二维码");
        qrCodeView = findViewById(R.id.iv_qrcode);
        nameView = findViewById(R.id.tv_name);
        avatarView = findViewById(R.id.iv_avatar);
        findViewById(R.id.tv_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ShareDialog(GroupQRCodeActivity.this).setOnShareListener(new ShareDialog.OnShareListener() {
                    @Override
                    public void wechatShare() {
                        ivWeChat(1, convertViewToBitmap(findViewById(R.id.rl_content)));
                    }

                    @Override
                    public void wechatCircleShare() {
                        ivWeChat(2, convertViewToBitmap(findViewById(R.id.rl_content)));
                    }

                    @Override
                    public void appShare() {
                        ShareUtils.getInstance().setShareBitmap(convertViewToBitmap(findViewById(R.id.rl_content)));
                        ActivityUtils.startActivity(ChatShareActivity.class);
                    }
                }).showPopupWindow(findViewById(android.R.id.content));
//                ShareDialog dialog = new ShareDialog();
//                dialog.setOnShareListener(new ShareDialog.OnShareListener() {
//                    @Override
//                    public void wechatShare() {
//                        ivWeChat(1,convertViewToBitmap(findViewById(R.id.rl_content)));
//                    }
//
//                    @Override
//                    public void wechatCircleShare() {
//                        ivWeChat(2,convertViewToBitmap(findViewById(R.id.rl_content)));
//                    }
//
//                    @Override
//                    public void appShare() {
//                        ShareUtils.getInstance().setShareBitmap(convertViewToBitmap(findViewById(R.id.rl_content)));
//                        ActivityUtils.startActivity(ChatShareActivity.class);
//                    }
//                });
//                dialog.show(getSupportFragmentManager(), "");
            }
        });
        long groupId = getIntent().getLongExtra("group_id", 0);
        JMessageClient.getGroupInfo(groupId, new GetGroupInfoCallback() {
            @Override
            public void gotResult(int i, String s, GroupInfo groupInfo) {
                nameView.setText(groupInfo.getGroupName());
                PicUtils.loadAvatarByGroup(groupInfo.getAvatarFile(),avatarView);
//                Glide.with(GroupQRCodeActivity.this).load().into();
            }
        });
        Bitmap logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        Bitmap bitmap = CodeCreator.createQRCode("jpush_group_id=" + groupId, 400, 400, logo);
        qrCodeView.setImageBitmap(bitmap);
    }

    private IWXAPI api;

    //分享到朋友圈或者朋友圈
    private void ivWeChat(int type, Bitmap bitmap) {
        api = DankalApplication.instance().getIWXAPI();
        WXImageObject imgObj = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

//设置缩略图
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
        bitmap.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction =String.valueOf(System.currentTimeMillis());
        req.message = msg;
        if (type == 1) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        } else {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }
//调用api接口，发送数据到微信
        api.sendReq(req);
    }

    public Bitmap convertViewToBitmap(View view) {

        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),

                Bitmap.Config.ARGB_8888);

//利用bitmap生成画布

        Canvas canvas = new Canvas(bitmap);

//把view中的内容绘制在画布上

        view.draw(canvas);

        return bitmap;

    }

}
